package com.aiju.base.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aiju.base.R;
import com.aiju.widgit.layout.AiJuTitleBarView;

/**
 * @auther: 佐为
 * @date: 2016.8.16
 * @Description: 带标题的fragment 基类
 */

public abstract class BaseTitleFragment extends BaseFragment{

    protected AiJuTitleBarView mAiJuTitleBarView;
    protected LinearLayout mContentContainer;

    protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGroup view = (ViewGroup) inflater.inflate(getFrameLayoutId(), null);
        mContentContainer = (LinearLayout) view.findViewById(R.id.ly_content);
        mAiJuTitleBarView = (AiJuTitleBarView) view.findViewById(R.id.toolbar);

        if (enableDefaultBack()) {
            mAiJuTitleBarView.setLeftOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            mAiJuTitleBarView.getLeftViewContainer().setVisibility(View.INVISIBLE);
        }
        View contentView = createView(inflater, view, savedInstanceState);
        if (contentView==mContentContainer)
            return view;
        contentView.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
        mContentContainer.addView(contentView);
        return view;
    }

    protected int getFrameLayoutId() {
        return R.layout.activity_base_title;
    }

    private void onBackPressed() {
        getContext().onBackPressed();
    }

    protected boolean enableDefaultBack() {
        return false;
    }

    protected void setCenterTitle(String title) {
        mAiJuTitleBarView.setCenterTitle(title);
    }

    public AiJuTitleBarView getTitleHeaderBar() {
        return mAiJuTitleBarView;
    }

}
